package cn.foodshopping.service.impl;

import cn.foodshopping.domain.Business;
import cn.foodshopping.domain.Goods;
import cn.foodshopping.mapper.BusinessMapper;
import cn.foodshopping.mapper.GoodsMapper;
import cn.foodshopping.service.IGoodsService;
import cn.foodshopping.util.MyException;
import cn.foodshopping.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
@Service
public class GoodsServiceImpl implements IGoodsService {
@Autowired
    GoodsMapper goodsMapper;
@Autowired
    BusinessMapper businessMapper;
    @Override
    public List<Goods> queryGoodsByType(Integer typeid) throws MyException {
        if (typeid==null){
            throw  new MyException("请输入typeid");

        }
        else {
            if(typeid==0){
                return goodsMapper.queryGoodsAll();
            }else {
                return goodsMapper.queryGoodsByType(typeid);
            }

        }
    }

    @Override
    public Goods queryGoodsById(Integer id) {
        if (id==null){
            System.out.println("调用queryGoodsByid(id) id=null");
            return null;
        }
        else {
            System.out.println("调用queryGoodsByid(id)");
            return goodsMapper.queryGoodsById(id);
        }
    }

    @Override
    public Result queryBusinessById(Integer businessId, Object goods) {
        if (businessId==null){

            return new Result(201,"ID不存在，请输入ID",null);
        } else if (goods==null){
            return new Result(202,"请求的数据为空",null);
        } else {


            Business business=businessMapper.queryBusinessById(businessId);

            System.out.println("business:");

            HashMap<String,Object> map=new HashMap<>();
            map.put("goods",goods);
            map.put("business",business);
            return new Result(200,"请求成功",map);
        }
    }

}
